﻿using System;
using System.Collections.Generic;
using System.Data;
using CMS.Utils;
using Newtonsoft.Json.Linq;

namespace CMS.Data
{
    [NeverRegiste]
    public static class Common

    {
        /// <summary>
        /// 过滤SQL数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToMySQLValue(this object obj, string formatDate = "yyyy-MM-dd HH:mm:ss.fff")
        {
            if (obj != null && obj.GetType().Name == "JValue")
            {
                obj = JValueToValue(obj);
            }
            string resultSQL = "";

            if (obj != null)
            {
                switch (obj.GetType().Name)
                {
                    case "DateTime":
                        var date = Convert.ToDateTime(obj).ToString(formatDate);
                        resultSQL = "'" + date.Replace("'", "''") + "'";
                        break;
                    case "String":
                    case "Boolean":
                        resultSQL = "'" + obj.ToString().Replace("'", "''") + "'";
                        break;
                    case "JObject":
                    case "JArray":
                        resultSQL = "'" + obj.ToString().Replace("'", "''") + "'";
                        break;
                    default:
                        resultSQL = obj.ToString();
                        break;
                }
            }
            else
                resultSQL = "null";
            return resultSQL;
        }



        private static object JValueToValue(object value)
        {
            JValue jvalue = value as JValue;
            object newObj = new object();
            newObj = jvalue;
            switch (jvalue.Type)
            {
                case JTokenType.Array:
                    break;
                case JTokenType.Boolean:
                    newObj = Convert.ToBoolean(jvalue.ToString());
                    break;
                case JTokenType.Bytes:
                    break;
                case JTokenType.Comment:
                    break;
                case JTokenType.Constructor:
                    break;
                case JTokenType.Date:
                    newObj = Convert.ToDateTime(jvalue.ToString());
                    break;
                case JTokenType.Float:
                    newObj = Convert.ToDouble(jvalue.ToString());
                    break;
                case JTokenType.Guid:
                    break;
                case JTokenType.Integer:
                    newObj = Convert.ToInt32(jvalue.ToString());
                    break;
                case JTokenType.None:
                    newObj = null;
                    break;
                case JTokenType.Null:
                    newObj = null;
                    break;
                case JTokenType.Object:
                    newObj = Newtonsoft.Json.JsonConvert.SerializeObject(jvalue);
                    break;
                case JTokenType.Property:
                    break;
                case JTokenType.Raw:
                    break;
                case JTokenType.String:
                    newObj = jvalue.ToString();
                    break;
                case JTokenType.TimeSpan:
                    break;
                case JTokenType.Undefined:
                    break;
                case JTokenType.Uri:
                    break;
                default:
                    break;
            }
            return newObj;
        }

        /// <summary>
        /// Table转Dictionary
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<Dictionary<string, object>> DataTableToDictionary(this DataTable dt)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            Dictionary<string, object> temp = null;
            foreach (DataRow row in dt.Rows)
            {
                temp = new Dictionary<string, object>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    temp.Add(dt.Columns[i].ColumnName, row[i] == DBNull.Value ? null : row[i]);
                }
                list.Add(temp);
            }
            return list;
        }

        /// <summary>
        /// 实体类转Dictionary
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static T ToObject<T>(this object obj)
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj, new Newtonsoft.Json.JsonSerializerSettings() { DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat });
            T newObj = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            return newObj;
        }
    }
}
